package com.zshan.clinic.web.controller.notice;


import com.zshan.clinic.admin.entity.vo.ClinicNoticeVo;
import com.zshan.clinic.admin.service.medical.ClinicFollowupService;
import com.zshan.clinic.admin.service.notice.ClinicNoticeService;
import com.zshan.clinic.admin.service.stock.StockDrugService;
import com.zshan.clinic.common.entity.BatchDTO;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.common.util.page.PageInfo;
import com.zshan.clinic.web.controller.AbstractController;
import com.zshan.clinic.web.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;


/**
 * 系统通知
 */
@RestController
@RequestMapping("/clinic/notice")
public class ClinicNoticeController extends AbstractController {


    @Autowired
    private ClinicNoticeService clinicNoticeService;

    @Autowired
    private ClinicFollowupService clinicFollowupService;

    @Autowired
    private StockDrugService stockDrugService;

    /**
     * 库存不足预警
     * @return
     */
    @GetMapping("/stockWarning")
    public RestResp<Void> stockWarning() {
        stockDrugService.stockWarning();
        return RestResp.SUCCESS();
    }

    /**
     * 今日未随访提醒
     * @param followupDate
     * @return
     */
    @GetMapping("/followupReminder")
    public RestResp<Void> followupReminder(@RequestParam(name = "followupDate",required = false) LocalDate followupDate) {
        clinicFollowupService.followupReminder(followupDate);
        return RestResp.SUCCESS();
    }

    /**
     * 查询消息未读数
     * @return
     */
    @GetMapping("/getUnreadNum")
    public RestResp<Integer> getUnreadNum() {
        UserInfo userInfo = getUserInfo();
        Integer unreadNum = clinicNoticeService.getUnreadNum(userInfo.getClinicId(),userInfo.getEmpId());
        return RestResp.SUCCESS(unreadNum);
    }

    /**
     * 消息列表
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getNoticeList")
    public RestResp<PageInfo<ClinicNoticeVo>> getNoticeList(@RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                            @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        PageInfo<ClinicNoticeVo> noticeList = clinicNoticeService.getNoticeList(userInfo.getClinicId(),userInfo.getEmpId(),pageNo,pageSize);
        return RestResp.SUCCESS(noticeList);
    }

    /**
     * 标记已读
     * @param batchDTO
     * @return
     */
    @PostMapping("/markRead")
    public RestResp<Void> markRead(@RequestBody BatchDTO batchDTO) {
        UserInfo userInfo = getUserInfo();
        batchDTO.setUserId(userInfo.getEmpId());
        batchDTO.setUserBy(userInfo.getEmpName());
        batchDTO.setClinicId(userInfo.getClinicId());
        clinicNoticeService.markRead(batchDTO);
        return RestResp.SUCCESS();
    }

    /**
     * 全部标记已读
     * @return
     */
    @PostMapping("/markAllRead")
    public RestResp<Void> markAllRead() {
        UserInfo userInfo = getUserInfo();
        BatchDTO batchDTO = new BatchDTO();
        batchDTO.setUserId(userInfo.getEmpId());
        batchDTO.setUserBy(userInfo.getEmpName());
        batchDTO.setClinicId(userInfo.getClinicId());
        clinicNoticeService.markAllRead(batchDTO);
        return RestResp.SUCCESS();
    }
    /**
     * 标记删除
     * @param batchDTO
     * @return
     */
    @PostMapping("/markDelete")
    public RestResp<Void> markDelete(@RequestBody BatchDTO batchDTO) {
        UserInfo userInfo = getUserInfo();
        batchDTO.setUserId(userInfo.getEmpId());
        batchDTO.setUserBy(userInfo.getEmpName());
        batchDTO.setClinicId(userInfo.getClinicId());
        clinicNoticeService.markDelete(batchDTO);
        return RestResp.SUCCESS();
    }
}
